package com.zu.commu.local.process;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.zu.commu.interf.Command;
import com.zu.commu.interf.Order;
import com.zu.commu.local.LocalServer;
import com.zu.commu.remote.session.SessionManager;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;

public class LocalProcess {
	private static Logger log = Logger
			.getLogger("com.automic.commu.local.process.LocalProcess");

	public void processData(SocketChannel channel, byte[] bs) {
		String str = "";

		try {
			String s = new String(bs).trim();
			if (Level.INFO.isGreaterOrEqual(log.getEffectiveLevel())) {
				log.info("Local线程得到的中心数据是：\n" + s);
			}
			if ((s == null) || (s.trim().length() == 0)) {
				log.error("从中心系统收到的数据是空串,关闭这个连接!");
				str = "从中心系统收到的数据是空串";
			} else {
				XStream xstream = new XStream(new DomDriver());
				xstream.alias("command", Command.class);

				Command command = (Command) xstream.fromXML(s);
				String orderStr = command.getOrder();
				Order order = new Order(orderStr);
				String gprsId = command.getGPRSID();

				if (order.equals(Order.ONLINE)) {
					List list = SessionManager.instance().getOnLineGprs();
					XStream xstream2 = new XStream(new DomDriver());
					xstream2.alias("list", ArrayList.class);
					str = xstream2.toXML(list);
				} else {
					LocalMeterProcess lmProcess = new LocalMeterProcess();
					str = lmProcess.processData(gprsId, order, command, bs);
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			str = "出错，出错信息：" + e.getMessage();

			byte[] rb = str.getBytes();
			ByteBuffer buffer = ByteBuffer.allocateDirect(rb.length);
			buffer.clear();
			buffer.put(rb);
			buffer.flip();
			try {
				channel.write(buffer);
				if (Level.INFO.isGreaterOrEqual(log.getEffectiveLevel())) {
					log.debug("返回中心的数据是：\n" + str);
				}
				try {
					Thread.sleep(100L);
				} catch (Exception localException1) {
				}
				SelectionKey seleKey = channel
						.keyFor(LocalServer.getSelector());
				if (seleKey != null) {
					seleKey.cancel();
				}
				channel.finishConnect();
				channel.socket().close();
				channel.close();
			} catch (Exception ex) {
				try {
					SelectionKey seleKey = channel.keyFor(LocalServer
							.getSelector());
					if (seleKey != null) {
						seleKey.cancel();
					}
					channel.finishConnect();
					channel.socket().close();
					channel.close();
				} catch (Exception ee) {
					ee.printStackTrace();
				} finally {
					channel = null;
				}
			}
		} finally {
			byte[] rb = str.getBytes();
			ByteBuffer buffer = ByteBuffer.allocateDirect(rb.length);
			buffer.clear();
			buffer.put(rb);
			buffer.flip();
			try {
				channel.write(buffer);
				if (Level.INFO.isGreaterOrEqual(log.getEffectiveLevel())) {
					log.debug("返回中心的数据是：\n" + str);
				}
				try {
					Thread.sleep(100L);
				} catch (Exception localException2) {
				}
				SelectionKey seleKey = channel
						.keyFor(LocalServer.getSelector());
				if (seleKey != null) {
					seleKey.cancel();
				}
				channel.finishConnect();
				channel.socket().close();
				channel.close();
			} catch (Exception e) {
				try {
					SelectionKey seleKey = channel.keyFor(LocalServer
							.getSelector());
					if (seleKey != null) {
						seleKey.cancel();
					}
					channel.finishConnect();
					channel.socket().close();
					channel.close();
				} catch (Exception ee) {
					ee.printStackTrace();
				} finally {
					channel = null;
				}
			}
		}
	}
}